package com.jy.api.service.impl;

import com.jy.api.dao.AppraiseDao;
import com.jy.api.domain.Appraise;
import com.jy.api.service.AppraiseService;
import com.jy.api.signUpReq.MarsCallReq.AppraiseEmpReq;
import com.jy.api.util.MyDateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by xiaohua on 2018/11/12.
 */
@Service
public class AppraiseServiceImpl implements AppraiseService {

    @Autowired
    private AppraiseDao appraiseDao;

    @Override
    public void appraiseEmp(AppraiseEmpReq req) {
        Appraise getAppraise = new Appraise(req);
        appraiseDao.save(getAppraise);
    }


    @Override
    public Page<Appraise> getEmpApp(String storeId, String type, String year, String month, String empId, Pageable pageable) {
        if (StringUtils.equals(type, "MONTH")) {
            String s = year + "-" + month;
            Date minMonthDate = MyDateUtils.getMinMonthDate(s);
            Date maxMonthDate = MyDateUtils.getMaxMonthDate(s);
            Page<Appraise> appraises;
            if (empId.equals("") || null == empId) {
                appraises = appraiseDao.findByStoreIdAndAppraiseTimeBetweenOrderByAppraiseTimeDesc(storeId, minMonthDate, maxMonthDate, pageable);
            } else {
                appraises = appraiseDao.findByStoreIdAndAppraiseTimeBetweenAndAndEmpIdOrderByAppraiseTimeDesc(storeId, minMonthDate, maxMonthDate, empId, pageable);
            }
            return appraises;
        } else {
            Page<Appraise> appraise = appraiseDao.findByStoreIdOrderByAppraiseTimeDesc(storeId, pageable);
            return appraise;
        }


    }
}
